package demo1;

import javafx.scene.input.PickResult;

public class Sort {
    //直接插入排序
    public void insertSort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            int j = i + 1;
            int tmp = array[j];
            for (; j > 0; j--) {
                if (tmp < array[j-1]) {
                    array[j] = array[j-1];
                } else {
                    break;
                }
            }
            array[j] = tmp;
        }
    }
    //直接插入排序
    //时间复杂度 : O(n^2)
    //空间复杂度 : O(1)
    public void insertSort1(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int j = i - 1;
            int tmp = array[i];
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j+1] = array[j];
                } else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    //希尔排序 : 对直接插入排序的优化
    // 时间复杂度 : O(n^1.3)
    public void shellSort(int[] array) {
        int gap = array.length / 2;
        while (gap > 0) {
            shell(array,gap);
            gap /= 2;
        }
    }
    public void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int temp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > temp) {
                    array[j+gap] = array[j];
                } else {
                    break;
                }
            }
            array[j+gap] = temp;
        }
    }
    //选择排序
    //时间复杂度 : O(n^2)
    //空间复杂度: (1)
    public void selectSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }
    //选择排序 优化

    public void selectSort1(int[] array) {
        for (int left = 0, right = array.length - 1; left < right; left++,right--) {
            int minIndex = left;
            int maxIndex = right;
            for (int i = left; i <= right; i++) {
                if (array[i] < array[minIndex]) {
                    minIndex = i;
                } else if (array[i] > array[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(array,maxIndex,right);
            minIndex = minIndex == right ? maxIndex : minIndex;
            swap(array,minIndex,left);
        }
    }

    //冒泡排序(稳定)
    //时间复杂度 : O(n^2)
    //空间复杂度 : O(1)
    public void bubbleSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            boolean flag = true;
            for (int j = 0; j < array.length - 1; j++) {
                if (array[j] > array[j+1]) {
                    swap(array,j,j+1);
                    flag = false;
                }
            }
            if (flag) {
                break;
            }
        }
    }

    //堆排序
    public void heapSort(int[] array) {
        //先把数组建立成大根堆
        createHeap(array);
        int usedSize = array.length - 1;
        while (usedSize > 0) {
           //把根节点(0下标的值)与useSize下标的值进行交换
           swap(array,usedSize,0);
           //交换完之后 恢复大根堆
            createHeapChild(array,0,usedSize);
            usedSize--;
        }
    }
    //建大根堆 (向下调整建堆 --> 时间复杂度为:O(n))
    private void createHeap(int[] array) {
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
            createHeapChild(array,parent,array.length);
        }
    }
    //向下调整 (时间复杂度 : O(logn))
    private void createHeapChild(int[] array,int parent,int len) {
        int child = parent * 2 + 1;
        while (child < len) {
            if (child + 1 < len && array[child] < array[child+1]) {
                child++;
            }
            if (array[child] > array[parent]) {
                swap(array,parent,child);
                parent = child;
                child = parent * 2 + 1;
            } else {
                break;
            }
        }
    }

    //快速排序
    public void quickSort(int[] array) {
        quickSortChild(array,0,array.length-1);
    }
    public void quickSortChild(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        //三数取中法
        int mid = findMid(array,start,end);
        swap(array,0,mid);

        if (start - end < 10) {
            insertSort(array);
            return;
        }

        int pivot = partition(array,start,end);
        quickSortChild(array,start,pivot - 1);
        quickSortChild(array,pivot + 1,end);
    }
    //三数去中法
    private int findMid(int[] array, int start, int end) {
        int mid = start + (end - start) / 2;
        if (array[start] > array[mid]) {
            if (array[start] < array[end]) {
                return start;
            } else if (array[mid] > array[end]) {
                return mid;
            } else {
                return end;
            }
        } else if (array[start] < array[mid]) {
            if (array[start] > array[end]) {
                return start;
            } else if (array[mid] > array[end]) {
                return end;
            } else {
                return mid;
            }
        } else {
            return start;
        }
    }

    //使用挖坑法 寻找基准值(pivot)
    public int partition(int[] array, int left, int right) {
        int temp = array[left];
        while (left < right) {
            if (left < right && array[right] >= temp) {
                right--;
            }
            array[left] = array[right];
            if (left < right && array[left] <= temp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = temp;
        return left;
    }

    //归并排序
    public void mergeSort(int[] array) {
        mergeSortChild(array,0,array.length-1);
    }
    public void mergeSortChild(int[] array,int start, int end) {
        if (start >= end) {
            return;
        }
        int mid = start + (end - start) / 2;
        mergeSortChild(array,start,mid);
        mergeSortChild(array,mid+1,end);
        merge(array,start,mid,end);
    }
    private void merge(int[] array, int start, int mid, int end) {
        int[] tmp = new int[end-start+1];
        int s1 = start;
        int s2 = mid + 1;
        int k = 0;
        while (s1 <= mid && s2 <= end) {
            if (array[s1] < array[s2]) {
                tmp[k++] = array[s1++];
            } else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= mid) {
            tmp[k++] = array[s1++];
        }
        while (s2 <= end) {
            tmp[k++] = array[s2++];
        }
        k = 0;
        for (int i = start; i <= end; i++) {
            array[i] = tmp[k++];
        }
    }
    //转换
    public void swap(int[] array, int x, int y) {
        int temp = array[x];
        array[x] = array[y];
        array[y] = temp;

    }

}
